package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void addCart(ShoppingCartDTO dto) {
        //创建缺失的对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        log.info("购物车数据:{}",shoppingCart);
        //1.判断该商品是否已经存在于购物车中
        //只查当前用户自己的购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart =  shoppingCartMapper.selectBy(shoppingCart);
        //2.补充缺失的属性值
        //判断是新增菜品还是新增套餐
        if (cart == null) {
            //购物车中不存在该商品
            if(dto.getDishId() != null){
                //新增的为菜品
                //根据菜品id查询菜品表
                DishDTO dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

            }else {
                //新增的为套餐
                SetmealDTO setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //将商品数据存入到shoppingCart表中
            shoppingCartMapper.insert(shoppingCart);
        }else {
            //购物车中已存在该商品
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }

    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    /**
     * 清空购物车
     */

    @Override
    public void cleanShoppingCart() {
        //需要根据当前用户ID来判断是哪个人的购物车，进行清空
        int count = shoppingCartMapper.cleanShoppingCart(BaseContext.getCurrentId());
        log.info("清空购物车成功，一共：{}条数据",count);
    }
    /**
     * 删除购物车中的一个商品
     * @param dto
     */

    @Override
    public void subCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        log.info("购物车数据:{}",shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart =  shoppingCartMapper.selectBy(shoppingCart);
        log.info("购物车数据:{}",cart);
        if(cart ==  null || cart.getNumber() == 0){
            throw new BaseException("购物车不存在该商品");
        }
        else {
            //购物车已经有该商品,找到了该商品
            //需要判断该商品的数量是否为为1，如果是1则需要将其从数据库中删除，如果不是则需要数量减1
            if(cart.getNumber() == 1){
                //购物车中该商品数量为1
                shoppingCartMapper.deleteBy(shoppingCart);
            }else {
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.update(cart);
            }
        }
    }
}
